import {
    fetch,
    AddUser,
    DeleteUser,
    FetchUser,
    UpdateUser
} from './service';
  
const Model = {
    namespace: 'manageandaccout',
    state: {
        
    },

    effects: {
        *fetch({ payload }, { call }) {
            const response = yield call(fetch, payload);
            if (!response.List) {
                return { status: 500 }
            }

            const data = []
            response.List.map(item =>{
                // 关联网关展示
                const showGateway = []
                // 关联网关表
                const gatewaysList = []
                item.Gateways.map(i => {
                    showGateway.push(i.Code)
                    gatewaysList.push(i)
                    return 200
                })
                // dacl true delete
                if (!item.Dacl) {
                    data.push({
                        key: item.Id,
                        id: item.Id,
                        user_accout: item.Accout,
                        user_name: item.Accout,
                        create_time: '-',
                        connec_gateway: showGateway,
                        gatewaysList
                        // connec_gateway: ['357404109951002','357404109951002','357404109951002'],
                    })
                }
                return 200
            })
            return {
                status: 200,
                totalPageSize: response.Page.TotalCount,
                data,
            }
        },

        *addUser({ payload }, { call }) {
            const data = {
                Name: payload.name,
                Accout: payload.accout,
                Password: payload.pwd,
                IsRoot: 0,
                Dacl: 0
            }
            const response = yield call(AddUser, data)
            return {
                status: 200, data: response
            }
        },

        *deleteUser({ payload }, { call }) {
            const data = {
                Id: payload.id,
                Accout: payload.user_accout,
                Dacl: 1
            }
            const response = yield call(DeleteUser, data)
            return {
                status: 200, data: response
            }

        },

        // FetchUser
        *updateUser({ payload }, { call }) {
            const data = {
                Id: payload.userId,
                id: payload.gatewayId,
            }
            const response = yield call(FetchUser, data)

            const gatewayList = []
            response[0].Gateways.map(item =>{
                if (data.id !== item.Id) {
                    gatewayList.push({Id: item.Id})
                }
                return 200
            })


            const newUser = {
                ...response[0],
                Gateways: gatewayList
            }

            const res = yield call(UpdateUser, newUser)

            return {status: 200, data: res}
        }

    },

    reducers: {
        
    },
}
export default Model;
  